package com.ufo.store.management.widget.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.ufo.store.management.R;
import com.ufo.store.management.adapter.Isszmbalv3BranchInfoRecyclerViewAdapter;
import com.ufo.store.management.entity.Isszmbalv3BranchInfo;

import java.util.List;

public class BranchDialog extends Dialog implements View.OnClickListener {
    private Context context;
    private OnItemClick mListener = null;
    private TextView mTitleTv;
    private String mTips;
    private String mCurrentSelectedBranchNo = "";
    private RecyclerView mRecyclerView = null;
    private List<Isszmbalv3BranchInfo> mIsszmbalv3BranchInfoList = null;
    private Isszmbalv3BranchInfoRecyclerViewAdapter mAdapter;
    private LinearLayoutManager mLinearLayoutManager;

    public BranchDialog(Context context) {
        super(context, R.style.select_dialog);
        this.context = context;
    }

    @Override
    protected final void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_branch);
        mTitleTv =  findViewById(R.id.tv_title);
        mRecyclerView = findViewById(R.id.branch_recyclerview);
        mLinearLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        mAdapter = new Isszmbalv3BranchInfoRecyclerViewAdapter(context);
        mAdapter.setOnClickListener(new Isszmbalv3BranchInfoRecyclerViewAdapter.OnClickListener() {
            @Override
            public void onItemClick(int index, String no, String name) {
                if( mListener != null ){
                    mListener.onItemClick(index, no, name);
                }

                new android.os.Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (null != BranchDialog.this && BranchDialog.this.isShowing()) {
                            BranchDialog.this.dismiss();
                        }
                    }
                }, 100);
            }

            @Override
            public void onItemLongClick(int position) {

            }
        });
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mRecyclerView.setAdapter(mAdapter);

        if (!TextUtils.isEmpty(mTips)) {
            mTitleTv.setText(mTips);
        }
        if( mIsszmbalv3BranchInfoList != null && mIsszmbalv3BranchInfoList.size() > 0 ){
            mAdapter.setBranchInfoList(mIsszmbalv3BranchInfoList);
            mAdapter.setCurrentSelectedBranchNo(mCurrentSelectedBranchNo);
            mAdapter.notifyDataSetChanged();
        }
    }

    public void setTips(String s) {
        mTips = s;
        if (mTitleTv != null && !TextUtils.isEmpty(mTips)) {
            mTitleTv.setText(mTips);
        }
    }

    public void setBranchInfoList( List<Isszmbalv3BranchInfo> list , String branchNo){
        mIsszmbalv3BranchInfoList = list;
        mCurrentSelectedBranchNo = branchNo;
        if( mIsszmbalv3BranchInfoList != null && mIsszmbalv3BranchInfoList.size() > 0 && mAdapter != null){
            mAdapter.setBranchInfoList(mIsszmbalv3BranchInfoList);
            mAdapter.setCurrentSelectedBranchNo(mCurrentSelectedBranchNo);
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onClick(View v) {
    }

    @Override
    public void setTitle(CharSequence title) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public void show() {
        if (context != null && ((Activity) context).isFinishing()) {
            return;
        }
        super.show();
    }

    public void setItemClickListener(BranchDialog.OnItemClick p) {
        mListener = p;
    }

    public interface OnItemClick {
        public void onItemClick(int index, String no, String name);
    }
}

